package dk.dtu.is.main.java;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

import dk.dtu.is.controllers.Iec61850HttpRestClient;
import dk.dtu.is.controllers.NodeParser;
import dk.dtu.is.model.iec61850.ln.Zcev;
import dk.dtu.is.model.settings.ClientSettings;
import dk.dtu.is.model.settings.Iec61850ServerSettings;
import dk.dtu.is.utility.settings.AppSettingsHelper;

public class RunPowerlabClientMVC {
	
	private AppSettingsHelper appSettingsHelper;
	private ClientSettings clientSettings;
	private Iec61850ServerSettings iec61850ServerSettings;
	
	public RunPowerlabClientMVC() {
		// read the settings
    	appSettingsHelper = new AppSettingsHelper();
    	clientSettings = appSettingsHelper.appSettings.getClientSettings();
    	iec61850ServerSettings = appSettingsHelper.appSettings.getIec61850ServerSettings();
	}
	
	/**
	 * Send the request to the server and get the response.
	 *  
	 * @return the response from the server
	 */
	public String getIec61850ServerResponse() {
		// this will contain the response, either from the server or from the local response file
		String response = null;
		
		// decide whether we have to load the internal file which resembles the response
    	// from the IEC 61850 server, or if we really have to send a request to the server  
    	if (clientSettings.getUseFakeServer()) {
    		// load and use the local file
    		// this is the full path of the settings file.
    		// it should be in the working directory
    		String localIec61850XmlFilePath = System.getProperty("user.dir") + 
    				System.getProperty("file.separator") + 
    				clientSettings.getLocalXmlResponseFileName();
    		
    		// read the file
    		File localIec61850XmlFile = new File(localIec61850XmlFilePath);
    		FileInputStream stream = null;
			try {
				stream = new FileInputStream(localIec61850XmlFile);
			    FileChannel fc = stream.getChannel();
			    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
			    /* Instead of using default, pass in a decoder. */
			    response = Charset.defaultCharset().decode(bb).toString();
			    stream.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
    	} else {
    		// form the full path of the resource we mean to access
    		String nodePath = "http://" + 
    				iec61850ServerSettings.getIpAddress() + 
    				":" +
    				iec61850ServerSettings.getPortNumber() +
    				"/" +
    				iec61850ServerSettings.getLogicalNode();
    		
    		// start up the http client and retrieve the resource from there
    		Iec61850HttpRestClient iec61850HttpRestClient = new Iec61850HttpRestClient();
    		iec61850HttpRestClient.init();
    		response = iec61850HttpRestClient.getNode(nodePath, "all", "", "");
    		iec61850HttpRestClient.close();
    	}
    	
    	return response;
	}
	
	/**
	 * Return the Zcev node contained in the response from the server
	 * 
	 * @param response The String which contains the response from the server
	 * @return the Zcev object with the values parsed from the response String
	 */
	public Zcev getZcevFromResponse(String response) {
    	NodeParser np = new NodeParser(response);
		return np.getZcev();
	}

}
